package put.cs.two.to.tsp.calculators.implementation.greedy;

import put.cs.two.to.tsp.calculators.base.AbstractNodeSetCalculator;
import put.cs.two.to.tsp.models.NodeSet;
import put.cs.two.to.tsp.models.Point;

public class CenterOfMassCalculator extends AbstractNodeSetCalculator {

    @Override
    public NodeSet doFilter(NodeSet inputNodes, int startingIndex) {
        try {
            tmpInputNodes = (NodeSet) inputNodes.clone();
        } catch (CloneNotSupportedException e) {
            return null;
        }
        NodeSet filteredNodes = new NodeSet();
        Point startingPoint = inputNodes.get(startingIndex);
        filteredNodes.add(startingPoint);
        tmpInputNodes.setUsed(startingPoint);
        Point massCenter;
        for (int i = 1; i < inputNodes.getDestinatedCycleLength(); ++i) {
            massCenter = filteredNodes.getMassCenter();
            Point nearest = getNearestPoint(massCenter);
            filteredNodes.add(nearest);
            tmpInputNodes.setUsed(nearest);
        }
        filteredNodes.setFiltered(true);
        return filteredNodes;
    }
}
